﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using rbac.api.Filters;
using rbac.contracts.Dto.Input;
using rbac.contracts.Dto.Output;
using rbac.contracts.Interface;
using rbac.domain.Impl;
using rbac.services;

namespace rbac.api.Controllers
{
    /// <summary>
    /// 权限
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class PermissionController : ControllerBase
    {
        IPermissionService _permissionService;

        public PermissionController(IPermissionService permissionService)
        {
            _permissionService = permissionService;
        }

        /// <summary>
        /// 权限查询
        /// </summary>
        /// <param name="roleNaem">角色名称</param>
        /// <returns></returns>
        [HttpGet]
        [RbacAuthorize("AdminAndCustomer")]
        public Results<List<PermissionOutput>> GetPermiss(string? permissionName)
        {
            return Results<List<PermissionOutput>>.DataResult(_permissionService.GetPermiss(permissionName));
        }

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost]
        [RbacAuthorize("Admin")]
        public Results<int> Add([FromBody] PermissionInput permission)
        {
            return Results<int>.DataResult(_permissionService.Add(permission));
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="idlist"></param>
        /// <returns></returns>
        [HttpDelete]
        [RbacAuthorize("Admin")]
        public Results<int> Delete([FromBody] List<int> idlist)
        {
            return Results<int>.DataResult(_permissionService.Delete(idlist.ToArray()));
        }
        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPut]
        [RbacAuthorize("Admin")]
        public Results<int> Update(PermissionInput permission)
        {
            return Results<int>.DataResult(_permissionService.Update(permission));
        }




    }
}
